<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">

<head>
<meta charset="UTF-8" />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- 引入 Bootstrap -->
<link href="./css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="js/kindeditor/themes/default/default.css" />
<script type="text/javascript" src="./js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script type="text/javascript" src="./js/kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="./js/kindeditor/lang/zh_CN.js"></script>
<script type="text/javascript">
	var editor;
	$(function() {
		//KindEditor4
		editor = KindEditor.create('textarea[id="addcontent"]', {
			allowFileManager : true
		});
	});
	function saveJournal() {
		$("#addcontent").text(editor.html());
		var title = $("#title").val();
		var content = $("#addcontent").text();
		if (title == "") {
			return;
		}
		if (content == "") {
			return;
		}
		$.ajax({
			type : "POST",
			url : "saveJournal",
			dataType : "json",
			data : {
				"title" : title,
				"content" : content,
				"type" : $("#journalType").val()
			},
			success : function(data) {
				if (data == "1") {
					alert("保存成功");
					$("#title").val("");
					editor.html("");
				} else {
					alert("保存失败");
				}
			},
			error : function(data) {
				alert("error" + data);
			}
		});
	}
	function queryJournals() {

		$.ajax({
			type : "POST",
			url : "findJournalsByTypeJson",
			dataType : "json",
			data : {
				"type" : $("#queryType").val()
			},
			success : function(data) {
				var trHtml = '';
				$.each(data, function(i, item) {
					trHtml += '<tr><td><a href="javascript:fun_journalDetail(\''+item.id+'\')">' + item.title + '</a></td><td>'
							+ formatDate(item.credate)
							+ '</td><td><a href="javascript:delJournal(\''
							+ item.id + '\')">删除</a></td></tr>';

				});
				$("#trsbody").html(trHtml);
			},
			error : function(data) {
				alert("error");
			}

		});
	}
	function fun_journalDetail(id){
		sessionStorage.setItem("manger_journal_id",id);
		location.href="journalManger.html";
	}
	function delJournal(id) {
		if (confirm("确定要删除?")) {
			var ckNum = prompt("验证");

			$.ajax({
				type : "POST",
				url : "delJournal",
				dataType : "json",
				data : {
					"id" : id,
					"ckNum" : ckNum
				},
				success : function(data) {
					if (data == "1") {
						alert("删除成功");
						queryJournals();
					} else {
						alert("删除失败");
					}
				},
				error : function(data) {
					alert("error");
				}

			});
		}

	}
	function addContentBtn() {
		$("#journalType").val($("#queryType").val());
		$("#jouranlQryDiv").hide();
		$("#addJournalDiv").show();
	}
	function qryJournalInfo() {
		$("#jouranlQryDiv").show();
		$("#addJournalDiv").hide();
		queryJournals();
	}
	function showContent() {
		$('#myModal').modal('show');
	}
	function formatDate(time) { //设置时间转换格式
		var date = new Date(time);
		var y = date.getFullYear(); //获取年
		var m = date.getMonth() + 1; //获取月
		m = m < 10 ? '0' + m : m; //判断月是否大于10
		var d = date.getDate(); //获取日
		d = d < 10 ? ('0' + d) : d; //判断日期是否大10
		return y + '-' + m + '-' + d; //返回时间格式
	}
</script>
</head>

<body>
	<div id="box" style="width: 90%; margin: 0 auto;">
		<div id="navbardiv">
			<nav class="navbar navbar-default " role="navigation">
				<div class="container-fluid">
					<div class="navbar-header">
						<a class="navbar-brand" href="#">峰</a>
					</div>
					<div>
						<ul class="nav navbar-nav">
							<li><a href="./blogIndex">首页</a></li>
						</ul>
					</div>
				</div>
			</nav>
		</div>
		<div id="content" style="margin-top: 3rem;">
			<div id="jouranlQryDiv">
				<div>
					<form role="form">
						<div class="form-group">
							<select id="queryType" class="form-control">
								<option value="work">工作</option>
								<option value="sentence">语句</option>
								<option value="book">书籍</option>
							</select> <input type="text" class="form-control" id="name"
								placeholder="请输入标题">

						</div>
					</form>

					<button type="button" onclick="queryJournals()"
						class="btn btn-primary" data-toggle="button">搜索</button>
					<button type="button" class="btn btn-primary"
						onclick="addContentBtn()" data-toggle="button">添加主题</button>
				</div>

				<table class="table table-striped">
					<caption>条纹表格布局</caption>
					<thead>
						<tr>
							<th>标题</th>
							<th>创建时间</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="trsbody">
					</tbody>
				</table>
			</div>
			<div id="addJournalDiv" style="display: none">
				<div class="panel panel-primary">
					<div class="panel-heading">
						<h3 class="panel-title">添加主题</h3>
					</div>
					<div class="panel-body"></div>
					<div id="">
						<button type="button" onclick="qryJournalInfo()"
							class="btn btn-primary" data-toggle="button">列表</button>
						<form class="form-horizontal" role="form">
							<div class="form-group">
								<label class="col-sm-2 control-label">类型</label>
								<div class="col-sm-10">
									<select id="journalType" class="form-control">
										<option value="work">工作</option>
										<option value="sentence">语句</option>
										<option value="book">书籍</option>
									</select>
								</div>
							</div>

							<div class="form-group">
								<label class="col-sm-2 control-label">标题</label>
								<div class="col-sm-10">
									<input type="text" class="form-control" id="title"
										placeholder="请输入名字">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">内容</label>
								<div class="col-sm-10">
									<textarea id="addcontent" name="content"
										style="width: 600px; height: 260px; visibility: hidden;"
										rows="5" cols="20"></textarea>
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-offset-2 col-sm-10">
									<button type="button" onclick="saveJournal()"
										class="btn btn-default">保存</button>
								</div>
							</div>
						</form>

					</div>
				</div>
			</div>

		</div>
	</div>
</body>
</html>